package com.mushuilingfeng.ddd.user.domain.org.entity;

import cn.hutool.core.util.StrUtil;
import com.mushuilingfeng.ddd.user.domain.base.BaseEntity;
import com.mushuilingfeng.ddd.user.domain.base.GenderEnum;
import com.mushuilingfeng.ddd.user.domain.org.repository.IDepartmentRepository;
import com.mushuilingfeng.ddd.user.domain.org.repository.IEmployeeRepository;
import com.mushuilingfeng.ddd.user.domain.org.repository.IOrgRepository;
import com.mushuilingfeng.ddd.user.domain.org.service.IEmployeeService;
import com.mushuilingfeng.ddd.user.domain.org.vo.Contact;
import com.mushuilingfeng.ddd.framework.factory.DomainServiceFactory;
import com.mushuilingfeng.ddd.framework.factory.DomainRepositoryFactory;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

/**
 * 员工
 *
 * @author
 * @date 2024/9/14
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class Employee extends BaseEntity {
    private String employeeCode;
    private String employeeName;
    private GenderEnum gender;
    private Contact contact;
    private String orgCode;
    private String deptCode;
    private Date joinTime;
    
    @Override
    public void saveNew() {
        if (StrUtil.isBlankIfStr(deptCode)) {
            throw new IllegalArgumentException("部门编码不能为空！");
        }
        if (StrUtil.isBlankIfStr(orgCode)) {
            throw new IllegalArgumentException("机构编码不能为空！");
        }
        if (StrUtil.isBlankIfStr(employeeName)) {
            throw new IllegalArgumentException("员工名称不能为空！");
        }
        if (null == contact || StrUtil.isBlankIfStr(contact.getTel())) {
            throw new IllegalArgumentException("手机号码不能为空！");
        }
        super.saveNew();
        if (null == employeeCode) {
            setEmployeeCode(DomainServiceFactory.getDomainService(IEmployeeService.class).newEmployeeCode());
        }
        DomainRepositoryFactory.getRepository(IEmployeeRepository.class).saveOrUpdate(this);
    }
    
    @Override
    public void delete() {
        DomainRepositoryFactory.getRepository(IEmployeeRepository.class).delete(id);
    }
    
    public Org getOrg() {
        return DomainRepositoryFactory.getRepository(IOrgRepository.class).getByOrgCode(orgCode);
    }
    
    public Department getDepartment() {
        return DomainRepositoryFactory.getRepository(IDepartmentRepository.class).getByDeptCode(deptCode);
    }
}
